package com.yc.xml;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/7/23
 * Email haijunzhou@hnit.edu.cn
 */
public class JsoupDemo {

    public static void main(String[] args) throws IOException {
        Document doc = Jsoup.parse(new URL("https://www.jiushujie.com/"), 5000);
        Elements as = doc.select(".book_list li>a");
        for (Element a : as) {
            String href = a.attr("href");
            href = "https://www.jiushujie.com" + href;
            doc = Jsoup.parse(new URL(href), 5000);
            String name = doc.selectFirst(".info>h2").text();
            String image = doc.selectFirst(".book_pic>img").attr("src");
            String detail = doc.selectFirst(".info>.detail").text();
            String[] strings = detail.split("[\\s/]+");
            String intro = doc.selectFirst(".book_introduction_container").html();
            System.out.println(Arrays.toString(strings));
            System.out.println("intro = " + intro);
        }

        // TODO 请将采集到的数据, 建表保存到数据库

        // TODO 采集电影港影片数据, 每个分类采集 50 个电影

    }






    public static void main1(String[] args) throws IOException {
        InputStream in = JsoupDemo.class.getClassLoader().getResourceAsStream("mybatis.xml");

        Document doc = Jsoup.parse(in, "utf-8", "");

        Elements environments = doc.getElementsByTag("environments");
        Element element = environments.get(0);
        String aDefault = element.attr("default");

        Element environment = doc.getElementById(aDefault);
        
        // 使用样式选择器获取 property
        //  select => js document.querySeletorAll
        // js document.querySeletor => selectFirst
        Elements properties = environment.select("property");
        for (Element property : properties) {
            String s = String.format("name = %s, value = %s \r\n",
                    property.attr("name"),
                    property.attr("value"));
            System.out.println(s);
        }

        // 扩展 TODO  解析 mapper 包含 resource 的内容
    }
}
